//////////////////////////////////////////////////////////////////////////////////
// hunter.c
// Implementation of decideMove(), AI to play 'Dracula's Fury' - Derpe's Inferno
// Author(s): Alexander Saunders
// TeamName: TheYellowFormPolicy
// Tutorial Group: Thursday
// Tutor: Sim
// Date: 19/01 22:01
// Version: 0
//////////////////////////////////////////////////////////////////////////////////


#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "hunterView.h"
#include "hunter.h"

//#include "game.h" ------------------------------------------------CHANGE THIS BACK

#define PLAYER_0_START 34 //Madrid
#define PLAYER_1_START 9  //Bucharest
#define PLAYER_2_START 36 //Marseilles
#define PLAYER_3_START 6  //Ich bin ein Berlinner


int hunterAI(HunterView hv);
void legalRoadMoves(int playerLoc[], HunterView hv);
//int maxRailDist(HunterView hv);
int findPlayerLoc(HunterView hv);
//int insortionStr(char *list[], char 
int decideMoveRand(int legalMovesNum);



   //------------------------------------------------is a void
int decideMove(HunterView gameState){
    
    int moveList[SEA_END + 1];
    int i;
    for (i = 0; i <= SEA_END + 1; i++){
        moveList[i] = 0;
    }
    //Find possible road moves
    legalRoadMoves(moveList, gameState);
    
    //"Decide" on a move
    char moveToCity[3];
    
    int moveListIndex = decideMoveRand(moveList[0]);

    int moveID = moveList[moveListIndex];
    getMoveCode(moveToCity, moveID);
    
    //playerMessage message = {""};------------------------------------------------uncomment
    //registerBestPlay(moveToCity , message)------------------------------------------------uncomment
    
    
    return moveID;
}
    
    


//Given the gamestate returns a list of all possible road moves
void legalRoadMoves(int moveList[], HunterView hv){
    
    if(turnNumber(hv) > 3){
        int playerLoc = findPlayerLoc(hv);
        printf("playerLoc = %i\n", playerLoc);
        
        //Declare adjacency matrix
        Connection **graph = malloc(sizeof(Connection*)*(SEA_END+1));
        int i, j;
        for (i = 0; i <= SEA_END; i++){
            graph[i] = malloc(sizeof(Connection)*(SEA_END+1));
            for (j = 0; j <= SEA_END; j++){
                graph[i][j] = malloc(sizeof(struct connection));
            }
        }
        
        giveGraph (hv, graph);
        
        printf("giveGraph BC->CN: %i\n", graph[locationCode("BC")][locationCode("CN")]->road);
        printf("giveGraph BC->GA: %i\n", graph[locationCode("BC")][locationCode("GA")]->road);
        printf("giveGraph BC->KL: %i\n", graph[locationCode("BC")][locationCode("KL")]->road);
        printf("giveGraph BC->SZ: %i\n", graph[locationCode("BC")][locationCode("SZ")]->road);
        printf("giveGraph BC->BE: %i\n", graph[locationCode("BC")][locationCode("BE")]->road);
        printf("giveGraph BC->SO: %i\n", graph[locationCode("BC")][locationCode("SO")]->road);
        
        //Copy legal moves to moveList[]
        j = 1;
        for (i = 0; i <= SEA_END; i++){
            if(graph[playerLoc][i]->road == 1){
                moveList[j] = i;
                j++;
                printf("i\n");
            }
        }
        
        //Element 0 contains the number of possible moves
        moveList[0] = j - 1;
    }else{
        //Special case for the first move
        int playerNum = whoAmI(hv);
        moveList[0] = 1;
        if(playerNum == 0){
            moveList[1] = PLAYER_0_START;
        }else if(playerNum == 1){
            moveList[1] = PLAYER_1_START;
        }else if(playerNum == 2){
            moveList[1] = PLAYER_2_START;
        }else{
            moveList[1] = PLAYER_3_START;
        }
    }
}

int findPlayerLoc(HunterView hv){
    
    Move lastMove[NUM_HUNTERS];
    int i;
    for (i = 0; i < NUM_HUNTERS; i++){
        lastMove[i] = malloc(sizeof (struct move));
    }

    movesSinceTurn(hv, lastMove);

    return lastMove[whoAmI(hv)]->location;
}

int decideMoveRand(int legalMovesNum){
    printf("LegalMovesNum = %i\n", legalMovesNum);
    srand(time(NULL));
    int moveNum;
    if(legalMovesNum > 1){
        moveNum = (rand() % (legalMovesNum + 1));
    }else{
        moveNum = 1;
    }
    return moveNum;
}
//Lana Del ray - you and I we were born to die
/*
int maxRailDist(HunterView hv){
    
    return (turnNumber(hv) + whoAmI(hv)) % 4;
    
}
*/

